home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_8 / issue_06 / risc_os / adfs / NewStruc < prev   
Encoding:
Text File  |  1988-10-03  |  12.3 KB  |  347 lines

  1.                 NEW DISC FILE STRUCTURE FOR ARTHUR 2
  2.                 ====================================
  3.  
  4. MOTIVATION
  5. ----------
  6. The aim of the new file structure is firstly to remove the following
  7. restrictions of the original file structure.
  8.  1. files must be stored contiguously on disc giving "compaction required"
  9.     and "can't extend" errors
  10.  2. there was a maximum number of entries in the free space map giving
  11.     "map full" error
  12.  3. defective sectors could not be mapped out of floppy discs
  13.  
  14. However most file structures which overcome these (eg MS-DOS and UNIX) pay a
  15. heavy penalty in performance for the following reasons. As files may be split
  16. up into several pieces the information on disc allocation is greatly
  17. increased. The structure(s) to keep track of free space and disc allocation
  18. are typically too large to be kept in RAM for hard discs. Therefore the disc
  19. allocation algorithms tend to be be designed to minimise scanning of these
  20. structures, rather than minimising fragmentation. This greatly speeds up the
  21. fragmentation of free space and files, as blocks are claimed and released.
  22. Even if utilities exist to rationalise disc structure an unintelligent
  23. allocation scheme quickly fragments things again. Fragmentation degrades
  24. performance since the parameters of standard disc drives are such that the
  25. time spent seeking between the fragments of a file will dominate the time
  26. spent transferring data unless the fragments are greater than a track length
  27. on average. Therefore if the unit of disc allocation is small fragmentation
  28. soon reduces most fragments to a few units in length giving slow performance,
  29. but if the allocation unit is big enough to give anything close to possible
  30. performance the allocation units are so big that a large part of the disc
  31. space is wasted in rounding up files to allocation units. These points led to
  32. the following additional aims.
  33.  
  34.  4. The disc allocation and free space structure(s) should be small enough to
  35.     be kept in RAM even for large hard discs.
  36.  
  37.  5. The allocation strategies should be intelligent to slow the rate of
  38.     fragmentation and should not produce small fragments.
  39.  
  40.  6. Long term fragmentation will still occur so the allocation strategy should
  41.     have the option of rationalising disc allocation, which should not produce
  42.     a noticeable delay to the user.
  43.  
  44.  
  45. STRUCTURE OVERVIEW
  46. ------------------
  47. The disc structure is made up of the following parts
  48.  
  49. 1. (hard discs only) A boot block containing the defect list and other
  50.    information
  51. 2. A double copied map which allows you to deduce for any cluster (unit of disc    allocation) whether it is allocated and if so to what position in which file
  52. 3. A hierarchical tree of directories
  53. 4. The remaining space is used for (fragments of) files or is unallocated
  54.  
  55. 1 and 3 are the same as for the old file structure (see OldMap document)
  56. except that disc addresses of files and directories are replaced by system
  57. internal numbers (SIN). The SIN includes a file number which can be looked up
  58. in the map to find the allocated disc space.
  59.  
  60.  
  61. NEW MAP STRUCTURE
  62. -----------------
  63. Most of the map is bitstream, with each bit in the map being associated with
  64. the allocation of a fixed number of bytes of disc space. This size (called
  65. the bit size) is always a power of two. The cluster size is the maximum of
  66. the bit size and the sector size. The map is divided into zones by the natural
  67. sector boundaries. Floppies are limited to one zone in the present
  68. implementation. The structure of a zone is as follows
  69.  
  70. start   byte
  71. byte    length  use
  72.  
  73.   0       1     this is a checksum on this zone calculated as below
  74.   1       2     offset in bits to first free space in zone, or 0 if none, with
  75.                 top bit always set
  76.   3       1     this byte when EORed with values for other zones should give FF
  77.   4      60     (only zone 0) the first 32 bytes are a disc record describing
  78.                 the disc, the other 28 bytes are 0, being reserved.
  79.  
  80. The rest of the zone is the bitstream. All entries in the bitstream have the
  81. same format. The start of an entry is an id field (of width given in the disc
  82. record), the end is marked by a 1, and any other bits are set to 0. The
  83. bitstream contains the following three types of object with their respective
  84. interpretation of the id field.
  85.  
  86.      OBJECT TYPE                ID VALUE
  87.  
  88.  a) (fragments of) files        file number (minimum value 2)
  89.  b) unallocated fragments       bit offset of next free fragment in zone or 0
  90.  c) disc defects                always 1
  91.  
  92. When searching for the fragments of a file zone containing the first fragment
  93. can be calculated from
  94.  
  95. zone = file number DIV ids per zone
  96.  
  97. Where ids per zone =  map bits in zone DIV (width of id field + 1)
  98.  
  99. An exception is the file number 2. This is reserved for the structure which is
  100. placed on an empty disc (boot block, map and root directory). Searching for
  101. file number 2 should start at zone = total zones DIV 2. This is to allow the
  102. root directory and map of hard disc to be placed near the middle.
  103.  
  104. The rest of the file's fragments (if any) can be found by searching through
  105. the zones in ascending order, wrapping back to zone 0 if necessary, looking
  106. for fragments with the correct file number. However because of the allocation
  107. strategies given below it is rare for a file to be in more than one piece
  108. unless it has been extended or is very large.
  109.  
  110. The need for an id field places a minimum size on a fragment. For example 
  111. possible fragment sizes are 2K, 3K, 4K, 5K ... for an 800K floppy, or 12K, 13K, 14K, 15K ... for a 20 Mb hard disc with a ten zone 2.5K map. Thus a compact
  112. map representation (down to 1 bit per cluster if necessary) has been achieved
  113. by making it impossible to represent inefficient allocations, (ie those with
  114. small fragments).
  115.  
  116. In order to avoid wasting disc space for small files, when a file or directory
  117. does not use the whole of its allocated fragment and the fragment is too small
  118. too split sharing is allowed. Directories can share fragments with their
  119. sub files and files in the same directory can share a fragment. Sharing is at
  120. sector rather than cluster resolution, saving disc space. This also improves
  121. performance, firstly because head movement is reduced since small files are
  122. closer to each other and their parent directory on average, and secondly the
  123. map does not always need to be modified for small files. 
  124.  
  125. The SIN of a file is a 24 bit value. Bits 0-7 are the sector offset+1 in the
  126. fragment if the file may share a fragment or 0 if not. Bits 8-23 are the file
  127. number.
  128.  
  129.  
  130. ALLOCATION STRATEGIES
  131. ---------------------
  132. Directories are allocated from the middle zone outwards and files produced by
  133. whole file operations are allocated from the zone containing their parent
  134. outwards. For hard discs as well as keeping files in the same directory close
  135. to each other and their parent directory, it also tends to keep small files
  136. near the middle of the disc and large files further out. Both these reduce head movement. When choosing disc space on openout it is put at the start of a zone
  137. which balances distance from parent directory with amount of free space in the zone.
  138.  
  139. As all allocation is indirected through the map it is possible to
  140. re-allocate to reduce fragmentation without having to read or write
  141. directories and a zone compaction routine is available for use by the
  142. allocation routines. This is highly optimised both in the choice of moves (eg
  143. it can spot fragments of the same file that can be joined) and in the
  144. execution of the moves. It builds up lists of moves which it cancels,
  145. combines, joins, splits, collects together in groups to be done together, and
  146. sorts into an order that reduces head movement for the scatter read/write
  147. primitives of the device drivers. Small compactions happen in response to
  148. particular allocation needs and opportunities rather than compacting a whole
  149. zone or disc at once so it not usually apparent to the user.
  150.  
  151. Files produced by whole file operations (eg SAVE) tend to be longer lived
  152. than those produced by partial file operations (eg OPENOUT, GBPB). So if
  153. they cannot be allocated a single extent compaction continues until a free
  154. space of the correct size or data totalling twice the length of the file has
  155. been moved. If compaction fails the file is allocated either a pair of
  156. extents which totals the correct size or a set of adjacent free spaces
  157. (possibly with an extent removed or added to give a better fit). The only
  158. partial file operations that do compaction are open and close, and then only
  159. if a very good opportunity is found. As files produced by whole file ops are
  160. almost always allocated a single extent, and long lived files produced by
  161. partial file ops tend to re-assemble their fragments as compactions happen,
  162. seeking between extents of a file is greatly reduced.
  163.  
  164.  
  165. MINOR DETAILS -------------
  166.  
  167. The checksum on a zone is calculated/checked as below
  168.  
  169. ;entry
  170. ; R0 -> start
  171. ; R1 zone length
  172.  
  173. ;exit
  174. ; LR check byte, Z=0 <=> good
  175.  
  176. NewCheck ROUT
  177.  Push   "R1,R2,LR"
  178.  MOV    LR, #0
  179.  ADDS   R1, R1, R0              ;C=0
  180. loop
  181.  LDR    R2, [R1, #-4] !
  182.  ADCS   LR, LR, R2
  183.  TEQS   R1, R0          ;preserves C
  184.  BNE    loop
  185.  AND    R2, R2, #&FF    ;ignore old sum
  186.  SUB    LR, LR, R2
  187.  EOR    LR, LR, LR, LSR #16
  188.  EOR    LR, LR, LR, LSR #8
  189.  AND    LR, LR, #&FF
  190.  CMPS   R2, LR
  191.  Pull   "R1,R2,PC"
  192.  
  193.  
  194.  
  195.  
  196. The checksum on directories remains the same. But the validation string for a
  197. directory has changed from 'Hugo' to 'Nick'
  198.  
  199.  
  200. ; Directory Start
  201.                 ^ 0
  202. StartMasSeq     # 1
  203. StartName       # 4
  204. DirFirstEntry   # 0
  205.  
  206. ; Old Directory End
  207.                 ^ 0
  208.                 # -1
  209. DirCheckByte    # 0     ;RETRO DEFINITION was reserved
  210.  
  211.                 # -4
  212. EndName         # 0
  213.  
  214.                 # -1
  215. EndMasSeq       # 0
  216.  
  217.                 # -14   ;reserved
  218.  
  219. DirTitleSz      * 19
  220.                 # -DirTitleSz
  221. OldDirTitle     # 0
  222.  
  223.                 # -3
  224. OldDirParent    # 0
  225.  
  226.                 # -NameLen
  227. OldDirName      # 0
  228.  
  229.                 # -1
  230. OldDirLastMark  # 0     ;dummy last entry marker
  231.  
  232. ; New Directory End
  233.                 ^ 0
  234.                 # -1
  235.         ASSERT  DirCheckByte=@
  236.  
  237.                 # -4
  238.         ASSERT  EndName=@
  239.  
  240.                 # -1
  241.         ASSERT  EndMasSeq=@
  242.  
  243.                 # -NameLen
  244. NewDirName      # 0
  245.  
  246.                 # -DirTitleSz
  247. NewDirTitle     # 0
  248.  
  249.                 # -3
  250. NewDirParent    # 0
  251.  
  252.                 # -1    ;reserved
  253.                 # -1    ;reserved
  254.  
  255.                 # -1
  256. NewDirLastMark  # 0     ;dummy last entry marker
  257.  
  258.  
  259.  
  260. ; ================
  261. ; TestDirCheckByte
  262. ; ================
  263.  
  264. ; entry
  265. ;  R3 ind disc add of dir
  266. ;  R5 -> dir start
  267. ;  R6 -> dir end
  268.  
  269. ; exit
  270. ;  LR check byte
  271. ;  Z  clear if matches existing byte
  272.  
  273. TestDirCheckByte
  274.  Push   "R0-R2,R5,R7,LR"
  275.  BL     EndDirEntries                   ;(R3,R5,R6->R0)
  276.  BL     TestDir                         ;(R3->LR,Z)
  277.  ADDEQ  R7, R6, #NewDirLastMark+1       ;dir tail
  278.  ADDNE  R7, R6, #OldDirLastMark+1
  279. 05
  280.  BIC    R1, R0, #3
  281.  MOV    R2, #0
  282. 10                              ;whole words before end of entries
  283.  LDR    LR, [R5],#4
  284.  EOR    R2, LR, R2,ROR #13
  285.  TEQS   R5, R1
  286.  BNE    %BT10
  287. 20                              ;odd bytes before end of entries
  288.  LDRNEB LR, [R5], #1            ;not first pass through loop
  289.  EORNE  R2, LR, R2,ROR #13
  290.  TEQS   R5, R0
  291.  BNE    %BT20
  292.  
  293.  MOV    R5, R7
  294. 30                              ;odd bytes before at start of tail
  295.  TSTS   R5, #3
  296.  LDRNEB LR, [R5],#1
  297.  EORNE  R2, LR, R2, ROR #13
  298.  BNE    %BT30
  299.  
  300.  ASSERT DirCheckByte=-1         ;dont include last word as it contains
  301.  SUB    R1, R6, #4              ;the check byte
  302. 40                              ;whole words in tail
  303.  LDR    LR, [R5],#4
  304.  EOR    R2, LR, R2,ROR #13
  305.  TEQS   R5, R1
  306.  BNE    %BT40
  307.  
  308.  EOR    R2, R2, R2, LSR #16     ;reduce to 8 bits
  309.  EOR    R2, R2, R2, LSR #8
  310.  AND    R2, R2, #&FF
  311.  
  312.  LDRB   LR, [R6,#DirCheckByte]  ;compare with old check byte
  313.  TEQS   R2, LR
  314.  MOV    LR, R2
  315.  
  316.  Pull   "R0-R2,R5,R7,PC"
  317.  
  318.  
  319. ; =============
  320. ; EndDirEntries
  321. ; =============
  322.  
  323. ; Find the end of the list of entries in a dir
  324.  
  325. ; entry
  326. ;  R3 ind disc add of dir
  327. ;  R5 -> dir start
  328. ;  R6 -> dir end
  329.  
  330. ; exit
  331. ;  R0 -> first empty entry
  332.  
  333. EndDirEntries ROUT
  334.  Push   "R2,LR"
  335.  BL     TestDir                 ;(R3->LR,Z)
  336.  ADDEQ  R2, R6, #NewDirLastMark
  337.  ADDNE  R2, R6, #OldDirLastMark
  338.  ADD    R0, R5, #DirFirstEntry
  339.  SUB    R0, R0, #DirEntrySz
  340. 10                              ;loop examining entries
  341.  LDRB   LR, [R0,#DirEntrySz] !
  342.  CMPS   LR, #0                  ;until null entry
  343.  CMPNES R0, R2                  ;or exhausted
  344.  BLO    %BT10
  345.  MOVHI  R0, R2
  346.  Pull   "R2,PC",,^
  347.